<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($specialist!=''){
	$querySpecialist = " AND SpecialistId = '".$specialist."' ";
}else{
	$querySpecialist = "";
}

$queryDayRount = " AND ShoutDay = '".$start."' AND ShoutRound = '".$items."' ";

	$sql="SELECT `TraderId`, `Price`, `PrivateValue` FROM x_shout WHERE ExperimentId = '".$Experiment."' ".$querySpecialist.$queryDayRount." ORDER BY Price ASC;";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$buyerId = array();
	$sellerId = array();
	$buyerPrice = array();
	$sellerPrice = array();
	$buyerPrivate = array();
	$sellerPrivate = array();
	

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$traderRole = substr($row['TraderId'], 0,3);

		if($traderRole=='buy'){
			$buyerId[] = str_replace('buyer_', '', $row['TraderId']);
			$buyerPrice[] = round($row['Price'],3);
			$buyerPrivate[] = round($row['PrivateValue'],3);

		}else if($traderRole=='sel'){
			$sellerId[] = str_replace('seller_', '', $row['TraderId']);
			$sellerPrice[] = round($row['Price'],3);
			$sellerPrivate[] = round($row['PrivateValue'],3);
		}
		
	}

	$sql2 ="SELECT t.Price 
		FROM x_shout as s, x_transaction as t 
		WHERE s.ExperimentId = '".$Experiment."'
		AND s.ExperimentId = t.ExperimentId 
		AND s.SpecialistId = '".$specialist."'
		AND s.ShoutDay = '".$start."' 
		AND s.ShoutRound = '".$items."'
		AND s.ShoutId =t.AskId";

	$result2 = mysql_query($sql2,$link) or die ("Can not read db!".mysql_error());

	while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){
		$tPrice = round($row2['Price']);
	}

$xFinal = max(count($buyerId), count($sellerId))-1;
$buyerId = array_reverse($buyerId);
$buyerPrice = array_reverse($buyerPrice);

$buyerPrivate = array_reverse($buyerPrivate);

//$buyerPrivate = natcasesort($buyerPrivate);
//$sellerPrivate = natcasesort($sellerPrivate);

$xLabel = array();
$price = array();
for($i=0; $i <= $xFinal; $i++){
	$xLabel[$i] = $buyerId[$i].' / '.$sellerId[$i];
	$price[$i] = $tPrice;
}

$line_buyerBid = new line_dot();
$line_buyerBid->set_width( 2 );
$line_buyerBid->set_colour( '#AEA725' );
$line_buyerBid->set_dot_size( 3 );
$line_buyerBid->set_values( $buyerPrice );
$line_buyerBid->set_tooltip( 'Buyer Bid: #val#' );
$line_buyerBid->set_key( "Buyer Bid", 10 );

$line_sellerAsk = new line_dot();
$line_sellerAsk->set_width( 2 );
$line_sellerAsk->set_colour( '#A66765' );
$line_sellerAsk->set_dot_size( 3 );
$line_sellerAsk->set_values( $sellerPrice );
$line_sellerAsk->set_tooltip( 'Seller Ask: #val#' );
$line_sellerAsk->set_key( "Seller Bid", 10 );

$line_buyerPrivate = new line_hollow();
$line_buyerPrivate->set_width( 1 );
$line_buyerPrivate->set_colour( '#AEA725' );
$line_buyerPrivate->set_dot_size( 5 );
$line_buyerPrivate->set_values( $buyerPrivate );
$line_buyerPrivate->set_tooltip( 'Buyer Private: #val#' );
$line_buyerPrivate->set_key( "Buyer Private", 10 );

$line_sellerPrivate = new line_hollow();
$line_sellerPrivate->set_width( 1 );
$line_sellerPrivate->set_colour( '#A66765' );
$line_sellerPrivate->set_dot_size( 5 );
$line_sellerPrivate->set_values( $sellerPrivate );
$line_sellerPrivate->set_tooltip( 'Seller Private: #val#' );
$line_sellerPrivate->set_key( "Seller Private", 10 );

$line_price = new line_hollow();
$line_price->set_width( 1 );
$line_price->set_colour( '#5EA725' );
$line_price->set_dot_size( 3 );
$line_price->set_values( $price );
$line_price->set_tooltip( 'price: #val#' );
$line_price->set_key( "price", 10 );


$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.' - Shout Price  -  '.$specialist.' (Day:'.$start.' Round:'.$final.')' ) );


$x_legend = new x_legend( 'Trader' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Price' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );


$x_labels = new x_axis_labels();
$x_labels->set_steps( 1 );
$x_labels->set_vertical();
//$x_labels->set_rotate( $rotate ) ;
$x_labels->set_colour( '#000000' );
$x_labels->set_labels( $xLabel );

$x = new x_axis();
$x->set_colour( '#333333' );
$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_labels( $x_labels );
$chart->set_x_axis( $x );

$minY =0 ;

$maxBuyerPrice = max($buyerPrice);
$maxSellerPrice = max($sellerPrice);
$maxY = round(max($maxBuyerPrice, $maxSellerPrice));
$maxY = $maxY+10-($maxY%10);

$differenceY = $maxY - $minY;
$divisorY = $differenceY / 10;
$stepY = $differenceY / $divisorY;

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( $minY, $maxY, $stepY ); 

//
// here we add our data sets to the chart:
//

$chart->add_element( $line_buyerBid );
$chart->add_element( $line_sellerAsk );
$chart->add_element( $line_buyerPrivate );
$chart->add_element( $line_sellerPrivate );
$chart->add_element( $line_price );

echo $chart->toPrettyString();

?>
